JavaScript Patterns by Stoyan Stefanov

JavaScript Patterns by Stoyan Stefanov

Author:Stoyan Stefanov [Stefanov, Stoyan]
Language: eng
Format: epub, pdf
Tags: Web, JavaScript, Programming Languages, COMPUTERS / Programming Languages / JavaScript, Computers, Web Programming, Lang:en
Publisher: O'Reilly Media
Published: 2012-11-10T11:55:16+00:00


Note

Adding convenient functionality to a language is often referred to as syntactic sugar or simply sugar. In this case, you could call the method() method a “sugar method.”

The way to define a “class” using the sugar method() would look like the following:

var Person = function (name) { this.name = name; }. method('getName', function () { return this.name; }). method('setName', function (name) { this.name = name; return this; });

Note how the constructor is chained to the call to method(), which is in turn chained to the next method() call and so on. This follows the chaining pattern previously described and helps you define the whole “class” with a single statement.

The method() takes two parameters:

The name of the new method



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.